<?php

namespace app\common\model;

use basic\ModelBasic;

class Pink extends ModelBasic
{
	use \traits\ModelTrait;

    public static $pink_status = [
        1 => '拼团成功',
        0 => '正在拼团'
    ];
    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field("m.*,u.full_name,u.avatar")
        ->join('__USER__ u','m.uid=u.uid' , 'left')
        ->page($where['page'] ?? 1,$where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    public static function getRelTypeAttr( $rel_type )
    {
        $mapping = [
            'package' => '套餐',
            'service' => '服务',
            'goods'   => '商品'
        ];
        return $mapping[$rel_type] ?? '-';
    }

    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->field("m.*")
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item = $item->toArray();

        return static::process_list([$item])[0];
    }

    public static function process_list (array $list)
    {
        foreach ( $list as &$item ) {
            $item['status_display']     = static::$pink_status[$item['status']] ?? '-';
            $item['pay_status_display'] = $item['paid_time'] > 0 ? '已支付' : '未支付';
        }
        return $list;
    }

	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);
        if ( isset($where['status']) && $where['status'] != '' ) {
            $model->where(sprintf('%s.status=%d' , $alias , $where['status']));
        }

        if ( isset($where['pid']) && $where['pid'] != '' ) {
            $model->where(sprintf('%s.pid|%s.id' , $alias , $alias) , $where['pid']);
        }

        if ( isset($where['rel_type']) && $where['rel_type'] != '' ) {
            $model->where(sprintf("%s.rel_type='%s'" , $alias , $where['rel_type']));
        }

        if ( isset($where['rel_id']) && $where['rel_id'] != '' ) {
            $model->where(sprintf('%s.rel_id=%d' , $alias , $where['rel_id']));
        }

        if ( isset($where['paid']) && $where['paid'] != '' ) {
            if ( $where['paid'] == 1 ) {
                $model->where($alias.'.paid_time' , '>' , 0);
            } else {

            }

        }

        if ( isset($where['date_time']) && strpos($where['date_time'] , ',') ) {
            list($start_time , $end_time) = explode(',' , $where['date_time']);
            if ( $start_time != '' ) {
                $model->where(sprintf("%s.create_time >= '%s'" , $alias , $start_time . ' 00:00:00'));
            }

            if ( $end_time != '' ) {
                $model->where(sprintf("%s.create_time <= '%s'" , $alias , $end_time . ' 23:59:59'));
            }
        }


        if ( isset($where['keyword']) && $where['keyword'] != '' ) {
            ## UID筛选
            $uids = User::where("full_name|mobile" , 'LIKE' , '%'.$where['keyword'].'%')->column('uid');
            $model->where($alias .'.uid' , 'IN' , $uids);
        }

        if ( static::$softDelete ) {
            $model->where($alias.'.is_del',0);
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.create_time DESC' , $alias));
        }

        return $model;
    }
}//Class End
